﻿using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;

namespace Himp.Platform
{
    /// <summary>
    /// 事件消息接收
    /// </summary>
    public class EventMsgRecvRecService : PlatformAppService
    {
        private readonly IRepository<EventMsgRecvRec, Guid> _msgSendRecRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgSendRecRepository"></param>
        /// <param name="hubContext"></param>
        public EventMsgRecvRecService(IRepository<EventMsgRecvRec, Guid> msgSendRecRepository)
        {
            _msgSendRecRepository = msgSendRecRepository;
        }

        /// <summary>
        /// 获取消息发送记录
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<EventMsgRecvRecDto>> GetListAsync(EventMsgRecvRecInput input)
        {
            if (input.TopicFullPath == null)
            {
                input.TopicFullPath = [];
            }

            var iQuery = await _msgSendRecRepository.GetQueryableAsync();
            iQuery = iQuery.Where( i => input.TopicFullPath.Contains(i.TopicFullPath));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<EventMsgRecvRec>, List<EventMsgRecvRecDto>>(iQuery.ToList());

                return new PagedResultDto<EventMsgRecvRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<EventMsgRecvRec>, List<EventMsgRecvRecDto>>(items.ToList());

                return new PagedResultDto<EventMsgRecvRecDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<EventMsgRecvRecDto> PostAsync(EventMsgRecvRecCreateDto input)
        {
            var eventMsgRecvRec = ObjectMapper.Map<EventMsgRecvRecCreateDto, EventMsgRecvRec>(input);
            eventMsgRecvRec = await _msgSendRecRepository.InsertAsync(eventMsgRecvRec);
            var itemDto = ObjectMapper.Map<EventMsgRecvRec, EventMsgRecvRecDto>(eventMsgRecvRec);

            return itemDto;
        }
    }
}
